package szcg.scheduled.service.api;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import szcg.framework.library.base.PostController;
import szcg.scheduled.service.service.TaskHandleService;

import java.util.HashMap;


@RestController
@RequestMapping(value = "/scheduled", produces = {"application/json;charset=UTF-8"})
public class SCHEDULED_001_000_000 extends PostController {
    private static final Logger logger = LoggerFactory.getLogger(SCHEDULED_001_000_000.class);

    private final TaskHandleService taskService;

    public SCHEDULED_001_000_000(TaskHandleService taskService) {
        this.taskService = taskService;
    }

    //session接口
    @PostMapping(value = "/001_000_000")
    public String doPost(HttpServletRequest request) {
        return handleRequest(request);
    }

    @Override
    protected String postRequest(HashMap<String, String> param) {
        String requestBody = param.get("requestBody");
        JSONObject jsonObject = (JSONObject) JSON.parse(requestBody);
        String action = jsonObject.getString("action");
        return switch (action) {
            case "0" -> taskService.createTask("100001", "0/3 * * * * ?", "定时推送案件任务");//删除任务
            case "1" -> taskService.startTask("100001"); //启动任务
            case "3" -> taskService.stopTask("100001"); //暂停任务
            case "2" -> taskService.deleteTask("100001");//删除任务
            case "4" -> taskService.editTask("100001", "0/5 * * * * ?", "这是修改的任务");//编辑/修改任务
            default -> generateFail("action错误");
        };
    }
}
